<template>
  <div class="list" ref="wrapper">
    <div>
      <!-- 当前城市 -->
      <div class="arer">
        <div class="titile border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-warper">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <!-- 热门城市 -->
      <div class="arer">
        <div class="titile border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-warper" v-for='item of hot' :key='item.id' @click='handleCityClick(item.name)'>
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="arer" v-for='(items,key) of cities' :key='key' :ref='key'>
        <div class="titile border-topbottom">{{key}}</div>
        <div class="item-list" v-for='item of items' :key='item.id' @click='handleCityClick(item.name)'>
          <div class="item border-bottom">{{item.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object,
    letter: String
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    letter: function () {
      if (this.letter !== '') {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },
  methods: {
    handleCityClick (city) {
      this.$store.commit('changeCity', city)
      this.$router.push('/')
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .list
    overflow: hidden
    position absolute
    top 1.58rem
    left 0px
    right 0px
    bottom 0px
    .border-topbottom
      &:before
        border-color: #ccc
      &:after
        border-color:#ccc
      .border-topbottom
      &:before
        border-color: #ccc
    .titile
      line-height .44rem
      background: #eee
      padding-left: .2rem
      color: #666
      font-size .26rem
    .button-list
      padding: .1rem .6rem .1rem .1rem
      overflow: hidden
      .button-warper
        width 33.33%
        float left
        .button
          text-align center
          margin: .1rem
          border: .02rem solid #ccc
          padding: 0.1rem 0
          border-radius 0.06rem
    .item-list
      .item
        line-height .8rem
        color: #666
        padding-left: .2rem
</style>
